package com.macoli.dynamicmockserver;

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatImageView
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.macoli.dynamicmockserver.converter.MapConverterFactory
import com.macoli.dynamicmockserver.interceptor.HttpLogger
import com.macoli.dynamicmockserver.login.ILoginService
import com.macoli.dynamicmockserver.login.IRegistService
import com.macoli.mock_server.MockService
import com.macoli.mock_server.httpserver.beans.Destnation
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import java.time.Duration


class MainActivity : AppCompatActivity() {

    private var retrofit : Retrofit? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initRetrofit()

        val basePath = getExternalFilesDir("")?.absolutePath + "/configuration_test/"
        val destnationList = ArrayList<Destnation>()

//        destnation.predicate = ::a
        MockService.startMockService(this , MainActivity::class.java , basePath , destnationList)




    }

    private fun loginDestnation(destnationList : ArrayList<Destnation>) {
        val destnation : Destnation = Destnation()
        destnation.uri = "/login"
        destnation.destnation = "/login_success"
        val loginPredicate : (params : Map<String, List<String>>) -> Boolean = {
            val a : Boolean = (it.get("username")?.get(0) == "menshen")
            val b : Boolean = (it.get("password")?.get(0) == "woshildx")
            a && b
        }
        destnation.predicate = loginPredicate
        destnation.destnation = "/login_success"
        destnationList.add(destnation)
    }

    fun initRetrofit() {
        val builder = OkHttpClient.Builder()
        builder.connectTimeout(Duration.ofSeconds(5))
        builder.readTimeout(Duration.ofSeconds(5))
        val logInterceptor = HttpLoggingInterceptor(HttpLogger())
        logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(logInterceptor)

        retrofit = Retrofit.Builder()
            .baseUrl("http://127.0.0.1:8080")
            .addConverterFactory(MapConverterFactory())
            .client(builder.build())
            .build()
    }

    fun login(view : View) {
        val apiService : ILoginService? = retrofit?.create(ILoginService::class.java)
        lifecycleScope.launch(Dispatchers.IO) {
            try {
                val result = apiService?.login("menshen" , "woshildx")?.execute()
                Log.d("gggl" , "login http response : ${result?.body()}")
            } catch (e : Exception) {
                e.printStackTrace()
            }

        }
    }

    fun regist(view : View) {
        val apiService : IRegistService? = retrofit?.create(IRegistService::class.java)
        lifecycleScope.launch(Dispatchers.IO) {
            try {
                val body = mapOf<String , String>(
                    "username" to "menshen" ,
                    "password" to "woshildx" ,
                    "phone" to "18888888888")
                val result = apiService?.regist(body)?.execute()
                Log.d("gggl" , "regist http response : ${result?.body()}")
            } catch (e : Exception) {
                e.printStackTrace()
            }

        }
    }

    fun static(view: View) {
        val img1 = findViewById<AppCompatImageView>(R.id.img1)
        val img2 = findViewById<AppCompatImageView>(R.id.img2)

        Glide.with(this).load("http://127.0.0.1:8080/123.png").into(img1);
        Glide.with(this).load("http://127.0.0.1:8080/456.png").into(img2);
    }


    fun a (params : Map<String, List<String>>) : Boolean {
        val a : Boolean = (params["username"]!!.get(0) == "menshen")
        val b : Boolean = (params["password"]!!.get(0) == "woshildx")
        if (a && b) {
            return true
        }
        return false
    }
}